package com.ruoyi.system.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.aliyun.oss.OSS;
import com.ruoyi.system.domain.TProduct;
import com.ruoyi.web.controller.config.OssConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TEquipment;
import com.ruoyi.system.service.ITEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2025-02-13
 */
@RestController
@RequestMapping("/system/equipment")
public class TEquipmentController extends BaseController {


    @Autowired
    private ITEquipmentService tEquipmentService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TEquipment tEquipment) {
        startPage();
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        return getDataTable(list);
    }

    //20250215-导出excel有进度条
    @PreAuthorize("@ss.hasPermi('system:equipment:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEquipment tEquipment) {
        List<TEquipment> list = tEquipmentService.selectTEquipmentList(tEquipment);
        ExcelUtil<TEquipment> util = new ExcelUtil<TEquipment>(TEquipment.class);

        // 创建一个字节数组输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // 将 Excel 数据写入字节数组输出流
        util.exportExcel(outputStream, list, "设备数据");

        // 获取字节数组
        byte[] excelBytes = outputStream.toByteArray();

        // 设置 Content-Length 头信息
        response.setContentLength(excelBytes.length);

        // 设置其他响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=product_export.xlsx");

        // 模拟慢速下载
        try (ServletOutputStream servletOutputStream = response.getOutputStream()) {
            int chunkSize = 1024; // 每次写入 1KB
            for (int i = 0; i < excelBytes.length; i += chunkSize) {
                int length = Math.min(chunkSize, excelBytes.length - i);
                servletOutputStream.write(excelBytes, i, length);
                servletOutputStream.flush();
                Thread.sleep(1000); // 延迟 100 毫秒
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tEquipmentService.selectTEquipmentById(id));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEquipment tEquipment) {
        return toAjax(tEquipmentService.insertTEquipment(tEquipment));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEquipment tEquipment) {
        return toAjax(tEquipmentService.updateTEquipment(tEquipment));
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tEquipmentService.deleteTEquipmentByIds(ids));
    }









}
